preview_mod = {
    allowedPages: [
        tuenti_lib.module.names.inicio,
        tuenti_lib.module.names.perfil,
        tuenti_lib.module.names.historial,
        tuenti_lib.module.names.pagina,
        tuenti_lib.module.names.foto,
        tuenti_lib.module.names.album,
        tuenti_lib.module.names.pm,
        tuenti_lib.module.names.gente,
        tuenti_lib.module.names.evento,
        tuenti_lib.module.names.miembros
    ],
    selector: '#friends div.avatar a[class=""], a.picture, div.avatar a, div.photo a, a.photo',
    init: function() {
        tuenti_lib.onchangepage.add(this, true);
    },
    onChangePage: function (response) {
        //fix: para el bug de cambio de pagina
        this.preview.hide();

        //miramos la configuracion
        var cfg = tuenti_lib.config.get();
        if (!cfg.preview) return;

        //chequeo urls de destino
        var url = (response ? response.request_object.url : document.location.href);
        var m = system_lib.url.getParam('m',  url);

        if (this.allowedPages.indexOf(m) == -1)
            return;

        var avatars = system_lib.select(this.selector);

        for (var i=0; i < avatars.length; i++) {
            avatars[i].addEventListener('mouseover', function () { preview_mod.preview.show(this); }, false);
            avatars[i].addEventListener('mouseout', function () { preview_mod.preview.stop(); }, false);
        }
    },
    preview: {
        id: 'tplus_vprev',
        scale: 2.7,
        minSize: 135,
        init: function() {
            var div = document.createElement('div');
            div.id = this.id;

            var a = document.createElement('a');
            var img = document.createElement('img');

            a.addEventListener('mouseout', function() {this.parentNode.style.display = 'none';}, false);

            img.onload = this.onloadimg;

            a.appendChild(img);
            div.appendChild(a);

            var overlay = document.getElementById('overlaysContainer');
            overlay.appendChild(div);
            
            this.content = div;
            this.content.a = a;
            this.content.img = img;
        },
        visible: function () {
            if (this.content && this.content.style.display === 'block')
                return true;
            return false;
        },
        hide: function () {
            if (this.content && this.content.style.display === 'block')
                this.content.style.display = 'none';
        },
        stop: function () {
            delete this.data;
            if (this.content && this.content.style.display === 'none')
                this.content.img.removeAttribute('src');
        },
        show: function (hover) {
            var oimg = hover.firstChild;

            var tsrc = tuenti_lib.visual.thumbUrl(oimg.src);

            if (!tsrc) return;

            //creamos el contenedor
            if (!this.content) this.init();

            var previewLink = this.content.a;
            var previewImg = this.content.img;

            //el preview tiene el enlace a la foto original
            previewLink.href = hover.href;
            //clonamos el evento onlick, algunas peticiones usan ajax y sino sera undefined asi que genial
            previewLink.onclick = hover.onclick;
            //calculamos el tamaño del link en base a la imagen original
            var thumbsize = Math.round(Math.max(Math.max(oimg.width, oimg.height) * this.scale, this.minSize));

            //asignamos variable temporal para el evento onloadimg
            this.data = {
                'oimg': oimg,
                'thumbsize': thumbsize
            };
            //Pasamos palabra al onload de la imagen

            //eliminamos atributos de tamaño
            previewImg.removeAttribute('width');
            previewImg.removeAttribute('height');

            previewImg.src = tsrc;
        },
        onloadimg: function() {
            var data = preview_mod.preview.data;
            if (!data) return;

            var oimg = data.oimg;
            var imgpos = system_lib.visual.realPosition(oimg);

            //fix: si se pulsa la imagen antes de generar el thum sale en la nueva pagina a veces
            if (!imgpos.x && !imgpos.y) {
                delete preview_mod.preview.data;
                return;
            }

            //Reescalamos manualmente con CSS da muchos problemas
            var thumb = system_lib.visual.thumbnalize(this.width, this.height, data.thumbsize, data.thumbsize);

            this.width = thumb.width;
            this.height = thumb.height;

            //Posicionamos el preview
            var x = Math.round(imgpos.x - thumb.width/2 + oimg.width/2);
            var y = Math.round(imgpos.y - thumb.height/2 + oimg.height/2);

            var preview = preview_mod.preview.content;
            preview.style.left = x + 'px';
            preview.style.top =  y + 'px';

            //mostramos el preview
            preview.style.display = 'block';

            delete preview_mod.preview.data;
        }
    }
};

preview_mod.init();